package cn.jingzhuan.fm.ui.activity;

import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;

import org.jetbrains.annotations.Nullable;

import cn.jingzhuan.fm.R;
import cn.jingzhuan.fm.ui.BaseActivity;

/**
 * Base Activity
 *
 * Created by donglua on 2017/5/10.
 */

public abstract class BaseBindingActivity<T extends ViewDataBinding> extends BaseActivity {

    protected T mBinding;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = createDataBinding(getLayoutId());


        onDataBind(savedInstanceState, mBinding);
    }

    protected abstract void onDataBind(@Nullable Bundle savedInstanceState, T binding);

    public T getBinding() {
        return mBinding;
    }

    public T createDataBinding(int layoutId) {
        return DataBindingUtil.setContentView(this, layoutId);
    }

    @LayoutRes
    protected abstract int getLayoutId();

    protected void showProgress(String msg) {
        if (isFinishing()) return;


    }

    protected void setUpActionBar(Toolbar toolbar){
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null)
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeAsUpIndicator(R.drawable.ico_back);
    }

    @Override public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home){
          onBackPressed();
          return true;
        }else {
          return super.onOptionsItemSelected(item);
        }
    }

}
